import axios from 'axios'
// eslint-disable-next-line import/no-absolute-path
import { menu } from '/jsonPlat/menu.json'

let url = ''
// 判断环境
switch (window.location.host) {
  case 'naturefw.gitee.io':
    url = '/nf-vue-cdn/elecontrol/json-plat/'
    break
  case 'localhost:8200':
    url = '/json-plat/'
    break
  case 'localhost:3000':
    url = '/public/json-plat/'
    break
}

/**
 * 通过模块ID和名称加载json文件
 * * 需要自己做缓存
 */
export default function loadjson (moduleId, name, meta) {
  return new Promise((resolve, reject) => {
    const currMenu = menu.filter((a) => a.id === moduleId)[0]
    // 加载
    axios.get(`${url}${currMenu.moduleFlag}/${name}.json`).then((res) => {
      // console.log('异步加载', res)
      if (res.status === 200) { // statusText
        // 正常
        Object.assign(meta, res.data)
        meta.reload = !meta.reload // 添加重新绑定的开关
        resolve(meta)
      }
    })
  })
}
